package org.black.missedCall2;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;

public class MissedCallConfirmClearMissedCallDialogFragment extends
		DialogFragment {

	public static final String TAG = "MissedCallConfirmClearMissedCallDialogFragmentTag";

	public static final boolean DEFAULT_DO_NO_ASK_ME_AGAIN_VALUE = false;

	public interface MissedCallConfirmClearMissedCallDialogListener {
		public void confirmToDeleteAllMissedCallRecords();
	}

	private MissedCallConfirmClearMissedCallDialogListener missedCallConfirmClearMissedCallDialogListener;

	public Dialog onCreateDialog(Bundle savedInstanceState) {
		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
		LayoutInflater inflater = getActivity().getLayoutInflater();
		View view = inflater.inflate(
				R.layout.missed_call_clear_call_log_dialog, null);
		builder.setView(view);

		final CheckBox doNotAskMeAgainCheckBox = (CheckBox) view
				.findViewById(R.id.missedCallClearAllRecordsDialogDoNotAskMeAgainCheckBox);
		builder.setTitle(R.string.clear_missed_call)
				.setPositiveButton(android.R.string.ok, new OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						boolean isChecked = doNotAskMeAgainCheckBox.isChecked();

						SharedPreferences sharedPreferences = PreferenceManager
								.getDefaultSharedPreferences(getActivity());
						Editor editor = sharedPreferences.edit();
						editor.putBoolean(
								getString(R.string.do_not_ask_me_again_preference_key),
								isChecked);
						editor.commit();
						missedCallConfirmClearMissedCallDialogListener
								.confirmToDeleteAllMissedCallRecords();
					}
				}).setNegativeButton(android.R.string.cancel, null);

		return builder.create();
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		try {
			missedCallConfirmClearMissedCallDialogListener = (MissedCallConfirmClearMissedCallDialogListener) activity;
		} catch (ClassCastException e) {
			throw new ClassCastException(
					activity.toString()
							+ " must implement MissedCallConfirmClearMissedCallDialogListener");
		}
	}
}
